const Router = require('koa-router');
const routes = require('./routes');
function initRouter(app) {
  let router;
  routes.forEach(route => {
    // 父路由有service时就使用router生成接口
    if (route.service) {
      router = new Router();
      router.all(route.path, route.service);
      app.use(router.routes());
    }
    if (route.children) {
      router = new Router({
        prefix: route.path
      })
      // 生成子路由
      let children = route.children;
      children.forEach(item => {
        router.all(item.path, item.service)
        app.use(router.routes());
      })
    }
  })
}

module.exports = initRouter;